﻿using System;
using System.Collections.Generic;

using SBPweb.Communication.Shared.Library;
using SBPweb.Communication.Shared.Library.Contacts;

using SBPweb.Communication.Client.Library.Xmpp.Commands;
using SBPweb.Communication.Client.Library.Xmpp.Connections;

namespace SBPweb.Communication.Client.Library.Xmpp.Contacts
{

    partial class ContactManager : ContactManagerBase
    {

        #region Private fields

        private Matrix.Xmpp.Roster.RosterManager rosterManager;
        private Matrix.Xmpp.Client.PresenceManager presenceManager;

        #endregion

        #region Property accessors

        #endregion

        #region Methods

        public override void GetContactList()
        {
            messenger.Commands.SendCommand(new GetRosterCommand(connections.localJid));
        }

        #region Event handlers

        #region Life cycle

        protected override void OnInitialize()
        {
            rosterManager = new Matrix.Xmpp.Roster.RosterManager(connections.client);
            presenceManager = new Matrix.Xmpp.Client.PresenceManager(connections.client);

            connections.client.AutoRoster = Configuration.GetContactListWhenConnected;

            connections.client.OnRosterStart += new EventHandler<Matrix.EventArgs>(OnClientRosterStart);
            connections.client.OnRosterEnd += new EventHandler<Matrix.EventArgs>(OnClientRosterEnd);
            connections.client.OnRosterItem += new EventHandler<Matrix.Xmpp.Roster.RosterEventArgs>(OnClientRosterItem);

            connections.client.OnPresence += new EventHandler<Matrix.Xmpp.Client.PresenceEventArgs>(OnClientPresence);

            connections.Connecting += new EventHandler(OnClientConnecting);
            connections.Connected += new EventHandler(OnClientConnected);
        }

        protected override void OnTerminate()
        {
            connections.Connected -= new EventHandler(OnClientConnected);
            connections.Connecting -= new EventHandler(OnClientConnecting);

            connections.client.OnPresence -= new EventHandler<Matrix.Xmpp.Client.PresenceEventArgs>(OnClientPresence);

            connections.client.OnRosterStart -= new EventHandler<Matrix.EventArgs>(OnClientRosterStart);
            connections.client.OnRosterEnd -= new EventHandler<Matrix.EventArgs>(OnClientRosterEnd);
            connections.client.OnRosterItem -= new EventHandler<Matrix.Xmpp.Roster.RosterEventArgs>(OnClientRosterItem);

            presenceManager = null;
            rosterManager = null;
        }

        #endregion

        #region Connection

        #endregion

        #region Roster items

        private void OnClientRosterStart(object sender, Matrix.EventArgs e)
        {
            contacts.Clear();
            base.OnContactListReceiving(sender, e);
        }

        private void OnClientRosterEnd(object sender, Matrix.EventArgs e)
        {
            base.OnContactListReceived(sender, e);
        }

        private void OnClientRosterItem(object sender, Matrix.Xmpp.Roster.RosterEventArgs e)
        {
            switch (e.RosterItem.Subscription)
            {
                default:
                case Matrix.Xmpp.Roster.Subscription.none:
                    break;
                case Matrix.Xmpp.Roster.Subscription.both:
                    contacts.Add(new Contact(e.RosterItem));
                    break;
            }
        }

        #endregion

        #region Presence changes

        private void OnClientPresence(object sender, Matrix.Xmpp.Client.PresenceEventArgs e)
        {
            if (e.Presence.From.Equals(connections.localJid))
            {
                return;
            }
            Contact contact = GetContactById(e.Presence.From.Bare) as Contact;
            if (contact != null)
            {
                contact.PresenceState = new ContactPresenceState(e.Presence);
                OnContactPresenceChanged(sender, new ContactPresenceStateBaseEventArgs(contact, contact.PresenceState));
            }
        }

        #endregion

        #endregion

        #endregion

    }

}